package and.roid.mushi;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class BoxView extends SurfaceView implements SurfaceHolder.Callback,
		Runnable {
	private SurfaceHolder holder;
	private Thread thread;
	// ゲーム状態
	private static final int GAME_START = 0;
	private static final int GAME_PLAY = 1;
	private static final int GAME_END = 2;
	private static final int GAME_CLEAR = 3;
	private int gamestate = 0;// ゲーム状態を表す変数
	private String massage = "";// 画面に表示させるメッセージ
	private float disp_w, disp_h;// 画面の幅高さ
	private MushiTataki mushitataki;
//	private BoxGamen gamen;
	private static final int// 画面の飾りのための座標
			gamen_x = 5,
			gamen_y = 5;
	private Bitmap img;

	
	
	// ゲーム状態
	private boolean endflg = false;// ゲームオーバーフラグ
	private boolean claflg = false;// クリアフラグ
	private static final double PIE = 3.1415926;
	private int gamen_x, gamen_y, gamen_w, gamen_h;// 画面の大きさ取得用
	private List<BoxOb> listBugs = new ArrayList<BoxOb>();
	private Bitmap[] imgs = new Bitmap[4];
	private int score = 0;// スコア用
	private int kazu;// 虫の数用
	
	// コンストラクタ
	public BoxView(Context context) {
		super(context);
		holder = getHolder();
		holder.addCallback(this);
		holder.setFixedSize(getWidth(), getHeight());
		init(context);
	}

	// 初期化処理
	public void init(Context context) {
		Resources resources = context.getResources();// 画像登録準備
		img = BitmapFactory.decodeResource(resources, R.drawable.mushirobo);// imgに画像登録
		mushitataki = (MushiTataki) context;
		disp_w = mushitataki.disp_w;// 画面幅取得
		disp_h = mushitataki.disp_h;// 画面高さ取得
		// BoxGamenクラスを作成
		gamen = new BoxGamen(gamen_x, gamen_y, (int) disp_w - 5,
				(int) disp_h - 35, img, gamestate);
		
		// 一旦ここで画像を４つに分ける
		for (int i = 0; i < imgs.length; i++) {
			imgs[i] = Bitmap.createBitmap(img, i * (img.getWidth() / 4), 0,
					img.getWidth() / 4, img.getHeight());
		}
		
	}

	// ループ：SurfaceViewのimprements Runnableを使用すると自動的に作成されるメソッド
	public void run() {
		Canvas c;
		Paint p = new Paint();
		p.setAntiAlias(true);

		while (thread != null) {
			c = holder.lockCanvas();
			c.drawColor(mushitataki.getcolor());
			// ゲーム状態によってスイッチ処理
			switch (gamestate) {
			case GAME_START:
				massage = "TOUCH GAME START";
				p.setTextSize(30);
				p.setColor(Color.GREEN);
				c.drawText(massage, gamen_x / 2, gamen_h / 2, p);
				break;
			case GAME_PLAY:// プレイ中にゲームオーバーかクリアかの判定
				massage = "";
				if (gamen.getEnd() == true) {
					gamestate = GAME_END;
					gamen.setGameState(gamestate);
				}
				if (gamen.getCla() == true) {
					gamestate = GAME_CLEAR;
					gamen.setGameState(gamestate);
				}
				p.setStyle(Paint.Style.STROKE);
				p.setColor(Color.BLACK);
				c.drawRect(new Rect(gamen_x, gamen_y, gamen_w, gamen_h), p);
				/*
				 * for (int i = 0; i < listBugs.size(); i++) {
				 * listBugs.get(i).draw(c, p); listBugs.get(i).setAnime();
				 * listBugs.get(i).move(); if (listBugs.get(i).getEnd() == true)
				 * endflg = true; if (listBugs.get(i).getDead() > 20) ++score; if
				 * (listBugs.get(i).getDead() > 20 || endflg == true) Reset(i); if
				 * (score == kazu) claflg = true; }
				 */
				synchronized (listBugs) {
					for (BoxOb bug : listBugs) {
						bug.draw(c, p);
						bug.setAnime();
						bug.move();
						if (bug.getEnd()) {
							endflg = true;
						}
						if (bug.getDead() > 20) {
							++score;
						}
						if (bug.getDead() > 20 || endflg) {
//							Reset(bug);
						}
						if (score == kazu) {
							claflg = true;
						}
					}
				}
				break;
			case GAME_END:
				massage = "虫の勝ち(´；ω；｀)";
				p.setTextSize(30);
				p.setColor(Color.RED);
				c.drawText(massage, gamen_x, gamen_h / 2, p);
				break;
			case GAME_CLEAR:
				massage = "GAMECLEAR!(*´∀｀*)";
				p.setTextSize(30);
				p.setColor(Color.RED);
				c.drawText(massage, gamen_x, gamen_h / 2, p);
				break;
			}

//			gamen.draw(c, p, massage);

			p.setTextSize(30);
			p.setColor(Color.BLACK);
			c.drawText("SCORE:" + score, 10, 30, p);
			
			holder.unlockCanvasAndPost(c);

			try {
				Thread.sleep(50);
			} catch (Exception e) {
			}
		}
	}

	// タッチ処理
	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		int x = (int) event.getX();
		int y = (int) event.getY();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			switch (gamestate) {
			case GAME_START:
				gamestate = GAME_PLAY;
//				gamen.setGameState(gamestate);
//				gamen.init(disp_w, img);
				break;
			case GAME_PLAY:
//				gamen.RectRect(x, y);
				for(BoxOb bug : listBugs){
					// 虫を潰したか？
					if(bug.isSmash(x, y)){
						// 潰したらスピードを0にする
						bug.setDeadSpeed();
					}
				}
				break;
			case GAME_END:
				gamestate = GAME_START;
				endflg = false;
//				gamen.setGameState(gamestate);
//				gamen.setEnd();
				break;
			case GAME_CLEAR:
				gamestate = GAME_START;
				claflg = false;
//				gamen.setGameState(gamestate);
//				gamen.setCla();
				break;
			}
			break;
		case MotionEvent.ACTION_UP:
			break;
		}
		return true;
	}

	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
	}

	public void surfaceCreated(SurfaceHolder arg0) {
		thread = new Thread(this);
		thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder arg0) {
		thread = null;
	}
	
}